package com.dkd.manage.controller;

import com.dkd.common.annotation.Log;
import com.dkd.common.core.controller.BaseController;
import com.dkd.common.core.domain.R;
import com.dkd.common.core.page.TableDataInfo;
import com.dkd.common.enums.BusinessType;
import com.dkd.common.utils.poi.ExcelUtil;
import com.dkd.manage.domain.Order;
import com.dkd.manage.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 订单管理Controller
 *
 * @author CoCo
 * @date 2025-02-06
 */
@Api(tags = "订单管理Controller")
@RestController
@RequestMapping("/manage/order")
public class OrderController extends BaseController
{
    @Autowired
    private IOrderService orderService;

    /**
     * 查询订单管理列表
     */
    @ApiOperation(value = "获取订单管理列表")
    @PreAuthorize("@ss.hasPermi('manage:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(Order order)
    {
        startPage();
        List<Order> list = orderService.selectOrderList(order);
        return getDataTable(list);
    }

    /**
     * 导出订单管理列表
     */
    @ApiOperation(value = "导出订单管理列表")
    @PreAuthorize("@ss.hasPermi('manage:order:export')")
    @Log(title = "订单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Order order)
    {
        List<Order> list = orderService.selectOrderList(order);
        ExcelUtil<Order> util = new ExcelUtil<Order>(Order.class);
        util.exportExcel(response, list, "订单管理数据");
    }

    /**
     * 获取订单管理详细信息
     */
    @ApiOperation(value = "获取订单管理详细信息")
    @PreAuthorize("@ss.hasPermi('manage:order:query')")
    @GetMapping(value = "/{id}")
    public R getInfo(@PathVariable("id") String id)
    {
        // 将id转为Long类型
        Long newId = Long.parseLong(id);
        Order order = orderService.selectOrderById(newId);
        return R.ok(order);
    }

    /**
     * 新增订单管理
     */
    @ApiOperation(value = "新增订单管理")
    @PreAuthorize("@ss.hasPermi('manage:order:add')")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody Order order)
    {
        return R.ok(orderService.insertOrder(order));
    }

    /**
     * 修改订单管理
     */
    @ApiOperation(value = "修改订单管理")
    @PreAuthorize("@ss.hasPermi('manage:order:edit')")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody Order order)
    {
        return R.ok(orderService.updateOrder(order));
    }

    /**
     * 删除订单管理
     */
    @ApiOperation(value = "删除订单管理")
    @PreAuthorize("@ss.hasPermi('manage:order:remove')")
    @Log(title = "订单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids)
    {
        // 遍历ids数组，将每个id转为Long类型
        Long[] newIds = new Long[ids.length];
        for (int i = 0; i < ids.length; i++) {
            newIds[i] = Long.parseLong(ids[i]);
        }
        return R.ok(orderService.deleteOrderByIds(newIds));
    }
}
